<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="lzy">
    <title>lzy-</title>
    <script src="jquery-3.1.1.js"></script>
    <style>
        ul,li{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
        }
        #box .list li{
            position: absolute;
            left: 0;
            right: 0;
            /*display: none;*/
        }
        #box .list li.current{
            display: block;
        }
        #box .count{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        #box .count li{
            float: left;
            width: 20px;
            height: 20px;
            background-color: #fa0;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            color: white;
            margin-left: 10px;
            opacity: 0.8;
            cursor: pointer;
        }
        #box .count li.current{
                background-color: red;
                opacity:1;
        }
        #box .count li:hover{
            background-color: #f60;
            opacity: 1;
        }
        span{
            display: inline-block;
            width: 25px;
            height: 50px;
            font-size: 24px;
            line-height: 50px;
            background-color: gray;
            opacity: 0.5;
            margin-top: 220px;
            margin-left: 10px;
            cursor: pointer;
        }
        span:hover{
            opacity: 0.8;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul class="list">
            <li class="current"><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>

        <span class="last"><</span>
        <span class="next">></span>
    </div>
</body>
</html>
<script>
    // 记录当前显示的图片的索引
    var current=0;
    // 保存定时器
    var timer=null;

$(document).ready(function(){
    timer=setInterval(autoplay,1000);

    // 自动
    function autoplay(){
        // console.log($('li').length);
        if(current<$('.list li').length-1){
            current++;
            $('.list li').eq(current).show();
            $('.list li').eq(current).siblings().hide();
        }
        else{
            current=0;
            $('.list li').eq(current).show();
            $('.list li').eq(current).siblings().hide();
        }
        console.log(current);
        console.log($('.count li').eq(current));
            $('.count li').eq(current).css({
                'backgroundColor':'red',
                'opacity':1,
            });
            $('.count li').eq(current).siblings().css({
                'backgroundColor':'#f60',
                'opacity':0.8,
            })
    }

    // 手动
    $('.count li').each(function(index){
        $(this).mouseover(function(){
            clearInterval(timer);   
            $('.list li').eq(index).show();
            $('.list li').eq(index).siblings().hide();
            $('.count li').eq(index).css({
                'backgroundColor':'red',
                'opacity':1,
            });
            $('.count li').eq(index).siblings().css({
                'backgroundColor':'#f60',
                'opacity':0.8,
            })           
        })
        $(this).mouseout(function(){
            timer = setInterval(autoplay,1000);
            current=$(this).index();
        })
    })

    $('span.next').mouseover(function(){
        clearInterval(timer);
        $(this).click(function(){
            autoplay();
        })
    })
    $('span.next').mouseout(function(){
        timer = setInterval(autoplay,1000);
    })
    $('span.last').mouseover(function(){
        clearInterval(timer);
        $(this).click(function(){
            autoplay2();
        })
    })
        

   


    // --
     function autoplay2(){
        if(current<0){
            current=$('.list li').length-2;
            $('.list li').eq(current).show();
            $('.list li').eq(current).siblings().hide();
        }
        else{
            current--;
            $('.list li').eq(current).show();
            $('.list li').eq(current).siblings().hide();
        }
        console.log(current);
        console.log($('.count li').eq(current));
            $('.count li').eq(current).css({
                'backgroundColor':'red',
                'opacity':1,
            });
            $('.count li').eq(current).siblings().css({
                'backgroundColor':'#f60',
                'opacity':0.8,
            })
        }  




});

</script>